package cn.tq365.service.soso;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.xml.namespace.QName;

import net.sf.json.JSONObject;

import org.apache.cxf.headers.Header;
import org.apache.cxf.jaxb.JAXBDataBinding;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.w3c.dom.Element;

import cn.tq365.mapper.ChannelAccountMapper;
import cn.tq365.model.channel.struct.ChannelAccount;
import cn.tq365.model.struct.soso.SoSoAccount;
import cn.tq365.service.channel.ChannelAccountService;
import cn.tq365.util.soso.TestConstants;

import com.soso.tg.api.advertise.accountservice.v1.AccountService;
import com.soso.tg.api.advertise.accountservice.v1.AccountService_Service;
import com.soso.tg.api.advertise.accountservice.v1.AdvertiserType;
import com.soso.tg.api.advertise.accountservice.v1.GetAdvertiserInfoRequest;
import com.soso.tg.api.advertise.accountservice.v1.GetAdvertiserInfoResponse;

/**
 * SOSO账户服务类
 * 
 * @author lili
 * 
 */
@Service
@Scope("prototype")
public class SoSoAccountService implements ChannelAccountService {
	@Resource
	ChannelAccountMapper soSoAccountMapper;

	@Override
	public void add(ChannelAccount account) throws Exception {
		try {
			AccountService_Service service = new AccountService_Service(
					new URL(TestConstants.WSDL_AccountService));
			AccountService port = service.getAccountServiceSOAP();

			// set header in request
			java.util.Map<String, Object> requestContext = ((javax.xml.ws.BindingProvider) port)
					.getRequestContext();
			List<Header> headers = new ArrayList<Header>();
			// there is no namespace for token and clientId
			Header tokenHeader = new Header(new QName("", "token"),
					TestConstants.TOKEN, new JAXBDataBinding(String.class));
			headers.add(tokenHeader);

			// 代理商和客服调用API，需要设置广告主Id，广告主无需设置
			Header clientIdHeader = new Header(new QName("", "clientId"),
					account.getName(), new JAXBDataBinding(String.class));
			headers.add(clientIdHeader);

			requestContext.put(Header.HEADER_LIST, headers);

			// invoke api
			GetAdvertiserInfoRequest request = new GetAdvertiserInfoRequest();

			GetAdvertiserInfoResponse response = port
					.getAdvertiserInfo(request);

			// parse response header
			java.util.Map<String, Object> responseContext = ((javax.xml.ws.BindingProvider) port)
					.getResponseContext();
			ArrayList<Header> responseHeaders = (ArrayList<Header>) responseContext
					.get(Header.HEADER_LIST);

			Header statuHeader = responseHeaders.get(0);
			String statusValue = ((Element) statuHeader.getObject())
					.getFirstChild().getNodeValue();
			// get status, 0=success, 1=fail, 2=part success
			// Assert.assertTrue(statusValue.equals("0"));
			// Assert.assertTrue(response.getCampaign().size() > 0);
			System.out.println(statusValue.equals("0"));
			// System.out.println(response.getCampaign().size());
			AdvertiserType remote = response.getAdvertiser();
			System.out.println(JSONObject.fromObject(remote));
			requestContext.put(Header.HEADER_LIST, headers);
			SoSoAccount acc = SoSoAccount.tranLocal(remote);
			soSoAccountMapper.add(acc);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

	}

	@Override
	public void addAll() throws Exception {
	}
}
